package com.situ.blog.controller;




import com.situ.blog.pojo.entity.User;
import com.situ.blog.service.IUserService;
import com.situ.blog.service.impl.UserServiceImpl;
import com.situ.blog.util.JSONUtil;
import com.situ.blog.util.Result;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

@WebServlet("/user")
public class UserServlet extends HttpServlet {
    private IUserService userService = new UserServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        // /user?method=selectByPage
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage":
                selectByPage(req, resp);
                break;
            case "login":
                login(req, resp);
                break;
            case  "logout":
                logout(req, resp);
                break;
            case  "add":
                add(req, resp);
                break;
            case "update":
                update(req, resp);
                break;
            case "toUpdate":
                toUpdate(req, resp);
                break;
            case "deleteById":
                deleteById(req, resp);
                break;
            case "register":
                register(req, resp);
                break;
        }
    }

    private void register(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        System.out.println("UserServlet.register");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.register(user);
        if (user == null) {
            JSONUtil.toJSON(resp, Result.error("注册失败！"));
        } else {
            JSONUtil.toJSON(resp, Result.ok("注册成功！"));
        }
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.deleteById");
        String id = req.getParameter("id");
        userService.deleteById(Integer.parseInt(id));
        resp.sendRedirect("/user?method=selectByPage");
    }

    private void toUpdate(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.toUpdate");
        String id = req.getParameter("id");
        User user = userService.selectById(Integer.parseInt(id));
        req.setAttribute("user", user);
        req.getRequestDispatcher("user_update.jsp").forward(req, resp);
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.update");
        String id = req.getParameter("id");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setId(Integer.parseInt(id));
        user.setUsername(username);
        user.setPassword(password);
        userService.update(user);
        resp.sendRedirect("/user?method=selectByPage");
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.add");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        User user = new User();
        user.setUsername(username);
        user.setPassword(password);
        userService.add(user);
        resp.sendRedirect("/user?method=selectByPage");
    }

    private void logout(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        System.out.println("UserServlet.logout");
        HttpSession session = req.getSession();
        session.removeAttribute("user");
        resp.sendRedirect("/login.jsp");
    }

    private void login(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.login");
        String username = req.getParameter("username");
        String password = req.getParameter("password");
        String code = req.getParameter("code");
        HttpSession session = req.getSession();
        String codeInSession = (String) session.getAttribute("codeInSession");
        if (!codeInSession.equalsIgnoreCase(code)) {
            JSONUtil.toJSON(resp, Result.error("验证码错误"));
            return;
        }
        User user = userService.login(username, password);
        if (user == null) {
            JSONUtil.toJSON(resp, Result.error("用户名或密码错误"));
        } else {
            session.setAttribute("user", user);
            JSONUtil.toJSON(resp, Result.ok("登录成功"));
        }
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("UserServlet.selectByPage");
        // /user?method=selectByPage
        String pageNo = req.getParameter("pageNo");
        String pageSize = req.getParameter("pageSize");
        if (pageNo == null || pageNo.equals("")) {
            pageNo = "1";
        }
        if (pageSize == null || pageSize.equals("")) {
            pageSize = "5";
        }
        //PageInfo pageInfo = userService.selectByPage(Integer.parseInt(pageNo), Integer.parseInt(pageSize));
        //System.out.println(pageInfo);
        //req.setAttribute("pageInfo", pageInfo);
        req.getRequestDispatcher("user_list.jsp").forward(req, resp);
    }
}
